package com.sne.airways.system.controller;

import com.sne.airways.basecore.controller.BaseController;
import com.sne.airways.basecore.exceptions.AppException;
import com.sne.airways.system.constants.Constant;
import com.sne.airways.system.dto.TdUserDTO;
import com.sne.airways.system.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-23
 * Time: 下午5:42
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @RequestMapping("/queryUserList")
    @ResponseBody
    public void queryUserList(String userNo,String userName,String status,int start,int limit){
        List<TdUserDTO> userDTOList = userService.queryUserList(userNo,userName,status,start,limit);
        int count = userService.queryUserCount(userNo,userName,status);
        renderPageJsonArray(userDTOList,count);
    }
    @RequestMapping("/saveUser")
     @ResponseBody
     public void saveUser(String opType,String userId,String userNo,String userName,String password,
                          String subsectionId,String userPhone,String userTel,String userMobile,
                          String userQQ,String userMsn,String userMail,String userRemark,String status,int version){
        //首先校验参数合法性
        if(StringUtils.isBlank(opType)||StringUtils.isBlank(userNo)||StringUtils.isBlank(userName)){
            throw new AppException("获取参数出错");
        }
        if(StringUtils.equals(Constant.ADD,opType)){
            if(StringUtils.isBlank(password)){
                throw new AppException("密码不能为空");
            }
            userService.saveUser(userNo, userName, password, subsectionId,
                    userPhone, userTel, userMobile, userQQ, userMsn, userMail, userRemark, status, version);
        }else if(StringUtils.equals(Constant.EDIT,opType)){
            if(StringUtils.isBlank(userId)){
                throw new AppException("用户主键不能为空");
            }
            userService.updateUser(userId, userNo, userName, password, subsectionId,
                    userPhone, userTel, userMobile, userQQ, userMsn, userMail, userRemark, status, version);
        }
        renderSuccess();
    }
    @RequestMapping("/deleteUser")
    @ResponseBody
    public void deleteUser(String opType,String userId,int version){
        //首先校验参数合法性
        if(StringUtils.isBlank(opType)||StringUtils.isBlank(userId)){
            throw new AppException("获取参数出错");
        }
        if(StringUtils.equals(Constant.DELETE,opType)){
            if(StringUtils.isBlank(userId)){
                throw new AppException("用户主键不能为空!");
            }
            userService.deleteUser(userId,version);
        }
        renderSuccess();
    }
}
